﻿using System;
using System.Data;
using System.Windows.Forms;
using 售后客户管理.Common;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms.InternalSetting
{
    public partial class FmSQLConfig : Form
    {
        public FmSQLConfig()
        {

        }
        public FmSQLConfig(ConfigCode ConfigCode)
        {
            InitializeComponent();
            RefrushConfigs(ConfigCode);
            Fmload();
            sqlConfig.ConfigCode = ConfigCode;
        }
        public static Configs configs = new Configs();
        public static SqlConfig sqlConfig = new SqlConfig();
        private SqlConnectionHelper sqlConnectionHelper = new SqlConnectionHelper(sqlConfig);

        private void Fmload()
        {
            //颜色方案
            //this.BackColor = MyColor.MyColor3;
            //this.ForeColor = MyColor.MyColor6;
            this.tbSQLAdd.BackColor = MyColor.MyColor5;
            this.tbSQLLgname.BackColor = MyColor.MyColor5;
            this.tbSQLLgpass.BackColor = MyColor.MyColor5;
            this.cbbDatabase.BackColor = MyColor.MyColor2;
            this.btnQuit.BackColor = MyColor.MyColor1;
            this.btnSave.BackColor = MyColor.MyColor1;
            this.label5.ForeColor = MyColor.MyColor7;

        }
        //mouse double click the tbSQLAdd to insert the default value
        private void tbSQLAdd_DoubleClick(object sender, EventArgs e)
        {
            if (this.tbSQLAdd.Text == "")
            {
                this.tbSQLAdd.Text = "127.0.0.1";
                this.tbSQLLgname.Text = "sa";
            }

        }
        //mouse double click the tbSQLLgname to insert the default value
        private void tbSQLLgname_DoubleClick(object sender, EventArgs e)
        {
            if (this.tbSQLAdd.Text == "")
            {
                this.tbSQLAdd.Text = "127.0.0.1";
                this.tbSQLLgname.Text = "sa";
            }
        }

        private void cbbDatabase_DropDown(object sender, EventArgs e)//select database drop-down options
        {
            //Write configuration of the application interface to the class
            SqlConfig sqlConfig = new SqlConfig();
            sqlConfig.Address = this.tbSQLAdd.Text.Trim();
            sqlConfig.DbName = this.cbbDatabase.Text.Trim();
            sqlConfig.LgName = this.tbSQLLgname.Text.Trim();
            sqlConfig.LgPass = this.tbSQLLgpass.Text.Trim();

            sqlConnectionHelper.SqlConfig = sqlConfig;
            //test link and write to combobox's drop-down menu
            if (sqlConnectionHelper.testLinks())
            {
                string sqls = @"SELECT name FROM master.sys.databases(nolock) WHERE name NOT IN('master','tempdb','model','msdb','ReportServer','ReportServerTempDB')";
                DataTable dt = SqlHelper.GetDataTable(sqls, sqlConnectionHelper.sqlConnection, null);
                this.cbbDatabase.DataSource = dt;
                this.cbbDatabase.DisplayMember = "name";
                this.cbbDatabase.ValueMember = "name";
                this.label5.Text = "Links Connect Success!";
            }
            else
            {
                MessageBox.Show("链接信息有误，请修改！！");
                this.tbSQLAdd.Focus();
            }

        }

        private void btnSave_Click(object sender, EventArgs e)//click Save button
        {
            //Write configuration of the application interface to the class
            SqlConfig sqlConfigA = new SqlConfig();
            sqlConfigA.Address = this.tbSQLAdd.Text.Trim();
            sqlConfigA.DbName = this.cbbDatabase.Text.Trim();
            sqlConfigA.LgName = this.tbSQLLgname.Text.Trim();
            sqlConfigA.LgPass = this.tbSQLLgpass.Text.Trim();

            //sqlConnectionHelper.sqlConfig = sqlConfig;
            if (sqlConnectionHelper.testLinks())//关闭设置窗口
            {
                this.label5.Text = "Links Connect Success!!!";
                sqlConfig = sqlConfigA;
                configs.Add(sqlConfigA);
                XmlHelper.XmlSerializeToFile(configs, AppSet.ConfigPath, AppSet.Encodings);
                this.Close();
                this.DialogResult = DialogResult.OK;
            }
            else//报错
            {
                if (MessageBox.Show("连接失败，请检查连接信息！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    this.tbSQLAdd.Focus();
                else
                    btnQuit_Click(btnQuit, new EventArgs());
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)//click Quit button
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        private void RefrushConfigs(ConfigCode configCode)//refrush the Configs to the application interface
        {
            if (System.IO.File.Exists(AppSet.ConfigPath))
            {
                configs = XmlHelper.XmlDeserializeFromFile<Configs>(AppSet.ConfigPath, AppSet.Encodings);
                foreach (SqlConfig config in configs)
                {
                    if (config.ConfigCode == configCode)
                    {
                        this.tbSQLAdd.Text = config.Address;
                        this.tbSQLLgname.Text = config.LgName;
                        this.tbSQLLgpass.Text = config.LgPass;
                        this.cbbDatabase.Text = config.DbName;
                    }
                }
            }
        }


    }
}
